package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	[SWF(width = "400",height = "480",backgroundColor = "0x212121")]
	public class TerisDemo extends Sprite
	{
		/**
		 *行数 
		 */		
		public static const rowNum:int = 20;
		/**
		 *列数 
		 */		
		public static const columNum:int = 10;
		/**
		 *格子大小 
		 */		
		private var size:int = 24;
		/**
		 *存储有无格子的数组 
		 */		
		private var cells:Array = [];
		private var color:uint;

		private var cluster:Cluster;

		private var timer:Timer;
		public function TerisDemo()
		{
			makeGrids()
			
			cluster = new Cluster();
			this.addChild(cluster);
			
			timer = new Timer(500);
			timer.addEventListener(TimerEvent.TIMER,update);
			timer.start();
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
				
		}
		
		protected function onKeyDown(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.LEFT :
					cluster.moveLeft();
					break;
				case Keyboard.RIGHT :
					cluster.moveRight();
					break;
				case Keyboard.UP :
					cluster.up();
					break;
				case Keyboard.DOWN :
					cluster.down();
					break;
				case Keyboard.SPACE :
					cluster.rotate();
					break;

			}
			
		}
		
		protected function update(event:TimerEvent):void
		{
//			cluster.down();
		}
		
		private function makeGrids():void
		{
			var g:Graphics = this.graphics;
			for(var row:int = 0;row<rowNum;row++)
			{	
				cells[row] = [];
				
				for(var colum:int = 0;colum<columNum;colum++)
				{	
					color = (row +colum)%2 == 0? 0x444444:0x555555
					g.lineStyle(0x0);
					g.beginFill(color);
					g.drawRect(size *colum,size *row,size,size);
					g.endFill();
					
					cells[row][colum] = 0;
				}
			}
			
		}
	}
}